# _*_ coding:utf-8 _*_
# @File  : task_scheduler.py
# @Time  : 2021-12-24  15:20
# @Author: zizle
import time
import json
import datetime
from .holidays import holidays
from .investment import daily_profit


def threading_task_scheduler():
    # 创建任务执行成功的记录文件
    """ 1. 读取处理过投资号收益的日期 """
    with open('./tasks/control_files/daily_profit.json', 'rb') as f1:
        daily_content = json.load(f1)

    # """ 2. 读取15分钟行情数据合约和处理过的时间 """
    # with open('./tasks/control_files/fifteen_quotes.json', 'rb') as f2:
    #     fifteen_quote_config = json.load(f2)

    while True:
        time.sleep(2)
        # 时间日期符合就运行
        """ 1. 每日处理投资号的收益情况 """
        current_week = datetime.datetime.today().weekday()
        current_date = datetime.datetime.today().strftime('%Y-%m-%d')
        if current_week >= 5 or current_date in holidays:  # 星期六日和节假日不执行
            continue

        current_time = datetime.datetime.today().strftime('%H:%M')
        if current_time >= '23:00' and current_date not in daily_content:  # 每日23点后执行一次
            daily_profit.generate_daily_profit()
            daily_content.append(current_date)
            write_json('./tasks/control_files/daily_profit.json', daily_content)  # 运行结束后写入文件,控制不再执行

        # """ 2. 请求同花顺15分钟行情数据 """
        # need_contracts = ','.join(fifteen_quote_config['contracts'])
        # need_datetime1 = f'{current_date} 09:15:00'
        # need_datetime2 = f'{current_date} 13:45:00'
        # need_datetime3 = f'{current_date} 21:15:00'
        # if '09:15:10' <= current_time <= '09:16:00' and need_datetime1 not in fifteen_quote_config['exist_time']:
        #     # (1)请求获取9:15分的行情
        #     success = fifteen_quote.handle_fifteen_contract_quotes(need_contracts, need_datetime1, need_datetime1)
        #     if success:
        #         fifteen_quote_config['exist_time'].append(need_datetime1)
        #         write_json('./tasks/control_files/fifteen_quotes.json', fifteen_quote_config)
        # if '13:45:10' <= current_time >= '13:46:00' and need_datetime2 not in fifteen_quote_config['exist_time']:
        #     # (2)请求获取13:45的行情
        #     success = fifteen_quote.handle_fifteen_contract_quotes(need_contracts, need_datetime2, need_datetime2)
        #     if success:
        #         fifteen_quote_config['exist_time'].append(need_datetime2)
        #         write_json('./tasks/control_files/fifteen_quotes.json', fifteen_quote_config)
        #
        # if '21:15:10' <= current_time >= '21:16:00' and need_datetime3 not in fifteen_quote_config['exist_time']:
        #     # (3)请求获取13:45的行情
        #     success = fifteen_quote.handle_fifteen_contract_quotes(need_contracts, need_datetime3, need_datetime3)
        #     if success:
        #         fifteen_quote_config['exist_time'].append(need_datetime3)
        #         write_json('./tasks/control_files/fifteen_quotes.json', fifteen_quote_config)


def write_json(filepath, content):
    with open(filepath, 'w', encoding='utf8') as fp:
        json.dump(content, fp)
